package com.jdy.haoduoai.adapter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoai.activity.BaseActivity;
import com.jdy.haoduoai.model.LePlaybackRecordItem;
import com.jdy.haoduoai.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

@SuppressLint("InflateParams")
public class LePlaybackRecordWrapperAdapter extends BaseAdapter {

  private BaseActivity mContext;
  private ArrayList<LePlaybackRecordItem> mList;
  private LayoutInflater mInflter;
  DisplayImageOptions options;

  private SparseArray<View> cvArray = new SparseArray<>();

  public LePlaybackRecordWrapperAdapter(BaseActivity context,
                                        ArrayList<LePlaybackRecordItem> list, String type) {
    this.mContext = context;
    this.mList = list;
    mInflter = LayoutInflater.from(mContext);
    // this.mType = type;

    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_img_listitem) // 下载 失败 显示的 图片
        .cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();

    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        mContext).writeDebugLogs().build();
    ImageLoader.getInstance().init(configuration);
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int position) {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LePlaybackRecordItem itemVo = mList.get(position);
    ViewHolder holder;

    convertView = cvArray.get(position);

    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflter.inflate(R.layout.playback_recent_item, null);
      holder.iv_channelicon = (ImageView) convertView
          .findViewById(R.id.iv_channelicon);
      holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
      convertView.setTag(holder);
      cvArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    if (Tools.isNotEmpty(itemVo)) {
      if (Tools.isNotEmpty(itemVo.catalog)) {
        if (itemVo.catalog.contains("cartoon")
            || itemVo.catalog.contains("radio")) {
          holder.tv_title.setText(itemVo.album_name);
        } else {
          holder.tv_title.setText(itemVo.name);
        }
      } else {
        holder.tv_title.setText(itemVo.album_name);
      }

      ImageLoader.getInstance().displayImage(itemVo.cover,
          holder.iv_channelicon, options);
    }

    return convertView;
  }

  private static class ViewHolder {
    private TextView tv_title;
    private ImageView iv_channelicon;

  }

}
